package com.hstc.service.impl;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hstc.feign.ProductFeignClient;
import com.hstc.order.bean.Order;
import com.hstc.product.bean.Product;
import com.hstc.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallback")
    @Override
    public Order createOrder(Long productId, Long userId) {
//        Product productFromRemote = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        //使用Feign进行远程调用
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("hstc");
        order.setProductList(Arrays.asList(product));
        return order;
    }
//    兜底回调
    public Order createOrderFallback(Long productId, Long userId, BlockException e) {
        Order order = new Order();
        order.setId(1L);
        order.setTotalAmount(new BigDecimal(0));
        order.setUserId(userId);
        order.setNickName("未知用户");
        order.setAddress("异常信息:"+e.getClass());
        return order;
    }


    private Product getProductFromRemote(Long productId){
        //获取商品服务所在的机器IP+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        //远程URL
        String url = "http://"+serviceInstance.getHost() + ":" + serviceInstance.getPort()+"/product/"+productId;
        log.info("远程请求：{}",url);
        //远程发送请求
        Product forObject = restTemplate.getForObject(url, Product.class);
        return forObject;
    }

    private Product getProductFromRemoteWithLoadBalance(Long productId){
        //获取商品服务所在的机器IP+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        //远程URL
        String url = "http://"+choose.getHost() + ":" + choose.getPort()+"/product/"+productId;
        log.info("远程请求：{}",url);
        //远程发送请求
        Product forObject = restTemplate.getForObject(url, Product.class);
        return forObject;
    }

    //基于注解的负载均衡
    private Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId){
        String url = "http://service-product/product/"+productId;
        //远程发送请求
        Product forObject = restTemplate.getForObject(url, Product.class);
        return forObject;
    }
}

